/**
 * This file is part of AppStoreBot.
 *
 *  AppStoreBot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AppStoreBot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AppStoreBot.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package lu.pragmaconsult.appstorebot.itunes;

import static lu.pragmaconsult.appstorebot.itunes.Constants.DATE_FORMAT;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import lu.pragmaconsult.appstorebot.BotContext;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO: Type comment
 * 
 * @author Wilfried Vandenberghe (wilfried.vandenberghe at pragmaconsult.lu)
 */
class ComputeReportDatesBotState extends AbstractBotState {

	private static final Logger logger = LoggerFactory
			.getLogger(ComputeReportDatesBotState.class);

	private boolean isWeekly = false;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * lu.pragmaconsult.appstorebot.itunes.AbstractBotState#createHttpMethod
	 * (lu.pragmaconsult.appstorebot.BotContext,
	 * org.apache.commons.httpclient.HttpClient)
	 */
	@Override
	protected HttpMethod createHttpMethod(BotContext context,
			HttpClient httpClient) {
		isWeekly = context.isWeekly();
		
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see lu.pragmaconsult.appstorebot.itunes.AbstractBotState#getNextState()
	 */
	@Override
	protected BotState getNextState() {
		if(isWeekly) {
			return new SelectWeeklyReportsBotState();
		} else {
			return new SelectDateBotState();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * lu.pragmaconsult.appstorebot.itunes.AbstractBotState#processResponseBody
	 * (lu.pragmaconsult.appstorebot.BotContext,
	 * org.apache.commons.httpclient.HttpMethod)
	 */
	@Override
	protected void processResponseBody(BotContext context, HttpMethod method) {
		if (context.isVerbose()) {
			logger.info("Downloading sales reports.");
		}

		// populate report days
		StringBuilder reportDates = new StringBuilder();
		if(context.getDate() != null && context.getDate().length() != 0) {
			reportDates.append(context.getDate());
		} else {
			SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
			
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			
			// set date to the next sunday
			if(isWeekly) {
				// 0 is SATHURDAY, not MONDAY
				int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
				calendar.add(Calendar.DAY_OF_YEAR, 8 - currentDay);
			}
			
			for(int i = 0; i < context.getDays(); i++) {
				if(isWeekly) {
					calendar.add(Calendar.WEEK_OF_YEAR, -1);
				} else {
					calendar.add(Calendar.DAY_OF_YEAR, -1);
				}
				if(reportDates.length() != 0) {
					reportDates.append(",");
				}
				reportDates.append(format.format(calendar.getTime()));
			}
		}
		
		context.setContextAttribute("reportDates", reportDates.toString());
		context.setContextAttribute("reportDatesIndex", "0");
	}

}
